JOBWD=`pwd`

source ${INPUT_1}

module purge
#module load intel
#module load intelmpi
#module load mkl/11.0.2
#module load hypre/2.9.0b
#module load mumps/4.10.0
#module load trilinos/11.0.3
#module load elmer/latest
#module load python-env
#module load gmsh
#module load python/2.7.9-gnu492  ## for sisu
module load python-env/intelpython2.7 ## for taito



OUTPUTSHARED=$shareddir



[[ -e $JOBWD/input ]] || ln -sv $STORAGESHARED/input $JOBWD/input
[[ -e $JOBWD/n_list.txt ]] || ln -sv $STORAGESHARED/n_list.txt $JOBWD/n_list.txt


first=`awk 'NR==1 {print $1}' n_list.txt`
# Current row
NR=`sed -n -e "$((COUNTER+1))p" n_list.txt`
nb=`awk '{print $1}' <<< $NR`



[[ -e $JOBWD/Particle_in ]] || ln -sv $OUTPUTSHARED/Particle_in $JOBWD/Particle_in


echo "Calving_$COUNTER : $JOBWD" >> $OUTPUTSHARED/worklfow-jobs.txt


mkdir -p $OUTPUTSHARED/ElmerResults

firstIteration=true
nrbefore=0
nb_before=0

nrafter=`sed -n -e "$((COUNTER+2))p" $JOBWD/input/n_list.bak`
nb_after=`awk '{print $1}' <<< $nrafter`

currentctr=$((COUNTER+1))

echo "COUNTER VARIABLE ::: $COUNTER"
echo "current counter ::: $currentctr"

if [ $currentctr -gt 1 ]; then
  echo "current ctr greater than one"
  nrbefore=`sed -n -e "$((COUNTER))p" $JOBWD/input/n_list.bak`
  nb_before=`awk '{print $1}' <<< $nrbefore`
  firstIteration=false
  echo "previous iteration element ::::  $nb_before"
fi


mkdir -p priorbeta/$nb

mkdir -p Python_particle_n/Date$nb



# Create ElmerResults for nb
mkdir -p ElmerResults/Date$nb
#mkdir -p ElmerResults/Date$nb/

#cd ElmerResults

# Run the particle model

echo "Run the particle model DUMMY .."
cd $JOBWD
mkdir -p $JOBWD/ParticleResults
mkdir -p $JOBWD/ParticleResults/Date$nb
#cd ParticleResults/Date$nb
mkdir -p $JOBWD/Particle_out

#[[ -e $OUTPUTSHARED/ParticleResults/Date$nb/maxi ]] || ln -sv $OUTPUTSHARED/ParticleResults/Date$nb/maxi $JOBWD/ParticleResults/Date$nb/maxi

if [ -e $OUTPUTSHARED/ParticleResults/Date$nb/maxi ]; then
  echo "maxi exists..."
  ln -sv $OUTPUTSHARED/ParticleResults/Date$nb/maxi $JOBWD/ParticleResults/Date$nb/maxi
fi

## you need to copy, otherwise python execution will 
## take the shared directory as ref.
## you need to copy, otherwise python execution will 
cp -rv $STORAGESHARED/input/ClassProperties-Template.py $JOBWD/input/Python_particle/ClassProperties.py
echo "modifying ClassProperties.py self.path_folder.."
sed -i -e 's|@@CWD@@|'${JOBWD}'|g' $JOBWD/input/Python_particle/ClassProperties.py


python $JOBWD/input/Python_particle/ParticleToElmer.py $nb $JOBWD

echo "copying last iteration's Date folder"
#mv $JOBWD/ElmerResults/Date$nb $OUTPUTSHARED/ElmerResults/
mkdir -p $OUTPUTSHARED/surf
cp -rv $JOBWD/surf/$nb_after $OUTPUTSHARED/surf/
